﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using BussinessLayer;
using DataAccessLayer;

namespace QLBaoHanh.QLDanhMuc
{
    public partial class frmDsNhanVien : Form
    {
        public frmDsNhanVien()
        {
            InitializeComponent();
        }

        #region Methods

        /// <summary>
        /// Add NhanVien
        /// Create and show form add
        /// </summary>
        private void AddNhanVien()
        {
            frmNhanVienDetail frmObj = SingletonFormProvider.GetInstance<frmNhanVienDetail>(this);
            frmObj.IsEditForm = false;
            //frmObj.MdiParent = this.ParentForm;
            frmObj.CallerForm = this;
            frmObj.ShowDialog();
            //frmObj.Focus();
        }

        /// <summary>
        /// Edit NhanVien
        /// Create and show form edit
        /// </summary>
        /// <param name="NhanVienId"></param>
        private void EditNhanVien(int NhanVienId)
        {
            if (NhanVienId == 0)
            {
                //Must choose a row
                MessageBox.Show("Phải chọn khách hàng!", "Ds khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                frmNhanVienDetail frmObj = SingletonFormProvider.GetInstance<frmNhanVienDetail>(this);
                frmObj.IsEditForm = true;
                frmObj.NhanVienId = NhanVienId;
                //frmObj.MdiParent = this.ParentForm;
                frmObj.CallerForm = this;
                frmObj.ShowDialog();
                //frmObj.Focus();
            }
        }

        /// <summary>
        /// Refesh form (grid)
        /// </summary>
        public void RefreshForm()
        {
            LoadDataToGrid();
            this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
        }
        /// <summary>
        /// Delete a NhanVien
        /// </summary>
        /// <param name="NhanVienId">NhanVienId</param>
        private void DeleteNhanVien(int NhanVienId)
        {
            if (NhanVienId == 0)
            {
                //Must choose a row
                MessageBox.Show("Phải chọn khách hàng!", "Ds khách hàng", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if(MessageBox.Show("Bạn chắc chắn muốn xóa?"
                    , "Khách hàng", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        NhanVienBO.getInstance.DelNhanVien(NhanVienId);
                    }
            }
        }

        /// <summary>
        /// Get selected item of grid view
        /// </summary>
        /// <returns>0 - not selected else return id</returns>
        private int getSelectedId()
        {
            int Id = 0 ;
            if (dataGridView1.SelectedRows.Count != 0)
            {
                Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            }
            return Id;
        }
        /// <summary>
        /// Load data to grid
        /// </summary>
        private void LoadDataToGrid()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = NhanVienBO.getInstance.GetAllNhanVien();
        }
        #endregion

        #region Events

        private void frmDSNhanVien_Load(object sender, EventArgs e)
        {
            LoadDataToGrid();   
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNhanVien();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditNhanVien(getSelectedId());           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteNhanVien(getSelectedId());
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditNhanVien(getSelectedId());
        }

        private void contextMenu_Add_Click(object sender, EventArgs e)
        {
            AddNhanVien();
        }

        private void contextMenu_Edit_Click(object sender, EventArgs e)
        {
            EditNhanVien(getSelectedId());
        }

        private void contextMenu_Delete_Click(object sender, EventArgs e)
        {
            DeleteNhanVien(getSelectedId());
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int rowSelected = e.RowIndex;
                if (e.RowIndex != -1)
                {
                    this.dataGridView1.Rows[rowSelected].Selected = true;
                }
            }
        }
        #endregion

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            // nothing is selected
            if (dataGridView1.SelectedRows.Count == 0)
                return;

            if (e.KeyCode == Keys.Enter)
            {
                EditNhanVien(getSelectedId());
                e.Handled = true;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value is NguoiDung && e.ColumnIndex == 2)
            {
                e.Value = (e.Value as NguoiDung).Username;
            }
            else if (e.Value is NguoiDung && e.ColumnIndex == 3)
            {
                e.Value = (e.Value as NguoiDung).VaiTro.Ten;
            }
        }

    }
}
